In [1]:
import os, random, glob
import numpy as np
import pandas as pd
from PIL import Image
from sklearn.model_selection import train_test_split
from sklearn.metrics import log_loss
from sklearn.preprocessing import LabelEncoder

import matplotlib.pyplot as plt
from matplotlib import ticker
import seaborn as sns
%matplotlib inline 

from keras.models import Sequential, Model, load_model
from keras.layers import GlobalAveragePooling2D, Flatten, Dropout, Dense
from keras.optimizers import Adam
from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau, TensorBoard
from keras.preprocessing.image import ImageDataGenerator
from keras.utils import np_utils
from keras import backend as K


Using TensorFlow backend.

In [2]:
TRAIN_DIR = '../data/train/'
TEST_DIR = '../data/test_stg1/'
FISH_CLASSES = ['ALB', 'BET', 'DOL', 'LAG', 'NoF', 'OTHER', 'SHARK', 'YFT']
ROWS = 224
COLS = 224
BatchSize = 64
LearningRate = 1e-4

In [3]:
#Loading data

import pickle

def get_images(fish):
    """Load files from train folder"""
    fish_dir = TRAIN_DIR+'{}'.format(fish)
    images = [fish+'/'+im for im in os.listdir(fish_dir)]
    return images

def read_image(src):
    """Read and resize individual images"""
    im = Image.open(src)
    im = im.resize((COLS, ROWS), Image.BILINEAR)
    im = np.asarray(im)
    return im
    
if os.path.exists('../data/data_train_{}_{}.pickle'.format(ROWS, COLS)):
    print ('Exist data_train_{}_{}.pickle. Loading data from file.'.format(ROWS, COLS))
    with open('../data/data_train_{}_{}.pickle'.format(ROWS, COLS), 'rb') as f:
        data_train = pickle.load(f)
    X_train = data_train['X_train']
    y_train = data_train['y_train']
else:
    print ('Loading data from original images. Generating data_train_{}_{}.pickle.'.format(ROWS, COLS))

    files = []
    y_train = []

    for fish in FISH_CLASSES:
        fish_files = get_images(fish)
        files.extend(fish_files)

        y_fish = np.tile(fish, len(fish_files))
        y_train.extend(y_fish)
        #print("{0} photos of {1}".format(len(fish_files), fish))

    y_train = np.array(y_train)
    X_train = np.ndarray((len(files), ROWS, COLS, 3), dtype=np.uint8)

    for i, im in enumerate(files): 
        X_train[i] = read_image(TRAIN_DIR+im)
        if i%1000 == 0: print('Processed {} of {}'.format(i, len(files)))

    #X_train = X_train / 255.
    #print(X_train.shape)

    # One Hot Encoding Labels
    y_train = LabelEncoder().fit_transform(y_train)
    y_train = np_utils.to_categorical(y_train)

    #save data to file
    data_train = {'X_train': X_train,'y_train': y_train }

    with open('../data/data_train_{}_{}.pickle'.format(ROWS, COLS), 'wb') as f:
        pickle.dump(data_train, f)

X_train, X_valid, y_train, y_valid = train_test_split(X_train, y_train, test_size=0.2, random_state=None, stratify=y_train)


Exist data_train_224_224.pickle. Loading data from file.

In [4]:
#data preprocessing

train_datagen = ImageDataGenerator(
    #featurewise_center=True,
    #featurewise_std_normalization=True,
    rescale=1./255,
    rotation_range=180,
    shear_range=np.pi/6.,
    zoom_range=[1,1.1],
    width_shift_range=0.1,
    height_shift_range=0.1,
    horizontal_flip=True,
    vertical_flip=True)

#train_datagen.fit(X_train)
train_generator = train_datagen.flow(X_train, y_train, batch_size=BatchSize, shuffle=True, seed=None)

valid_datagen = ImageDataGenerator(rescale=1./255)

valid_generator = valid_datagen.flow(X_valid, y_valid, batch_size=BatchSize, shuffle=True, seed=None)

In [5]:
#callbacks

early_stopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=1, mode='auto')        

model_checkpoint = ModelCheckpoint(filepath='./checkpoints/weights.{epoch:03d}-{val_loss:.4f}.hdf5', monitor='val_loss', verbose=1, save_best_only=True, save_weights_only=False, mode='auto')
        
learningrate_schedule = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=5, verbose=1, mode='auto', epsilon=0.001, cooldown=0, min_lr=0)

tensorboard = TensorBoard(log_dir='./logs', histogram_freq=0, write_graph=True, write_images=True)

In [ ]:
#stg1 training

from keras.applications.vgg16 import VGG16

optimizer = Adam(lr=LearningRate)

base_model = VGG16(weights='imagenet', include_top=False)

x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(256, init='glorot_normal', activation='relu')(x)
#x = Dropout(0.5)(x)
x = Dense(256, init='glorot_normal', activation='relu')(x)
#x = Dropout(0.5)(x)
predictions = Dense(len(FISH_CLASSES), init='glorot_normal', activation='softmax')(x)

# this is the model we will train
model = Model(input=base_model.input, output=predictions)

# first: train only the top layers (which were randomly initialized)
# i.e. freeze all convolutional VGG16 layers
for layer in base_model.layers:
    layer.trainable = False

# compile the model (should be done *after* setting layers to non-trainable)
model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])

# train the model on the new data for a few epochs
model.fit_generator(train_generator, samples_per_epoch=len(X_train), nb_epoch=300, verbose=1, 
                    callbacks=[early_stopping, model_checkpoint, learningrate_schedule, tensorboard], 
                    validation_data=valid_generator, nb_val_samples=len(X_valid), nb_worker=3, pickle_safe=True)


Epoch 1/300
3008/3021 [============================>.] - ETA: 0s - loss: 1.7936 - acc: 0.3873
/opt/anaconda3/lib/python3.5/site-packages/keras/engine/training.py:1470: UserWarning: Epoch comprised more than `samples_per_epoch` samples, which might affect learning results. Set `samples_per_epoch` correctly to avoid this warning.
  warnings.warn('Epoch comprised more than '
Epoch 00000: val_loss improved from 1.54322 to 1.53578, saving model to ./checkpoints/weights.000-1.5358.hdf5
3072/3021 [==============================] - 82s - loss: 1.7922 - acc: 0.3867 - val_loss: 1.5358 - val_acc: 0.4714
Epoch 2/300
3008/3021 [============================>.] - ETA: 0s - loss: 1.5833 - acc: 0.4591Epoch 00001: val_loss improved from 1.53578 to 1.53013, saving model to ./checkpoints/weights.001-1.5301.hdf5
3072/3021 [==============================] - 65s - loss: 1.5797 - acc: 0.4619 - val_loss: 1.5301 - val_acc: 0.4714
Epoch 3/300
2983/3021 [============================>.] - ETA: 0s - loss: 1.6090 - acc: 0.4398Epoch 00002: val_loss improved from 1.53013 to 1.50639, saving model to ./checkpoints/weights.002-1.5064.hdf5
3047/3021 [==============================] - 67s - loss: 1.6069 - acc: 0.4411 - val_loss: 1.5064 - val_acc: 0.4766
Epoch 4/300
3008/3021 [============================>.] - ETA: 0s - loss: 1.5359 - acc: 0.4584Epoch 00003: val_loss did not improve
3072/3021 [==============================] - 66s - loss: 1.5391 - acc: 0.4580 - val_loss: 1.5418 - val_acc: 0.4466
Epoch 5/300
3008/3021 [============================>.] - ETA: 0s - loss: 1.5323 - acc: 0.4638Epoch 00004: val_loss improved from 1.50639 to 1.41786, saving model to ./checkpoints/weights.004-1.4179.hdf5
3072/3021 [==============================] - 65s - loss: 1.5339 - acc: 0.4626 - val_loss: 1.4179 - val_acc: 0.5221
Epoch 6/300
2983/3021 [============================>.] - ETA: 0s - loss: 1.4921 - acc: 0.4844Epoch 00005: val_loss did not improve
3047/3021 [==============================] - 66s - loss: 1.4901 - acc: 0.4851 - val_loss: 1.4232 - val_acc: 0.4922
Epoch 7/300
3008/3021 [============================>.] - ETA: 0s - loss: 1.4787 - acc: 0.5020Epoch 00006: val_loss improved from 1.41786 to 1.41012, saving model to ./checkpoints/weights.006-1.4101.hdf5
3072/3021 [==============================] - 66s - loss: 1.4769 - acc: 0.5029 - val_loss: 1.4101 - val_acc: 0.5286
Epoch 8/300
3008/3021 [============================>.] - ETA: 0s - loss: 1.3000 - acc: 0.5648Epoch 00016: val_loss did not improve
3072/3021 [==============================] - 66s - loss: 1.2957 - acc: 0.5677 - val_loss: 1.3306 - val_acc: 0.5443
Epoch 18/300
2983/3021 [============================>.] - ETA: 0s - loss: 1.3347 - acc: 0.5488Epoch 00017: val_loss did not improve
3047/3021 [==============================] - 66s - loss: 1.3310 - acc: 0.5504 - val_loss: 1.3372 - val_acc: 0.5234
Epoch 19/300
3008/3021 [============================>.] - ETA: 0s - loss: 1.2928 - acc: 0.5598Epoch 00018: val_loss improved from 1.30461 to 1.26809, saving model to ./checkpoints/weights.018-1.2681.hdf5
3072/3021 [==============================] - 66s - loss: 1.2915 - acc: 0.5605 - val_loss: 1.2681 - val_acc: 0.5742
Epoch 20/300
3008/3021 [============================>.] - ETA: 0s - loss: 1.3017 - acc: 0.5522Epoch 00019: val_loss did not improve
3072/3021 [==============================] - 66s - loss: 1.3029 - acc: 0.5508 - val_loss: 1.2750 - val_acc: 0.5885
Epoch 21/300
2983/3021 [============================>.] - ETA: 0s - loss: 1.3027 - acc: 0.5525Epoch 00020: val_loss did not improve
3047/3021 [==============================] - 66s - loss: 1.3029 - acc: 0.5517 - val_loss: 1.2806 - val_acc: 0.5768
Epoch 22/300
3008/3021 [============================>.] - ETA: 0s - loss: 1.2563 - acc: 0.5691Epoch 00021: val_loss improved from 1.26809 to 1.25889, saving model to ./checkpoints/weights.021-1.2589.hdf5
3072/3021 [==============================] - 67s - loss: 1.2577 - acc: 0.5664 - val_loss: 1.2589 - val_acc: 0.5964
Epoch 23/300
3008/3021 [============================>.] - ETA: 0s - loss: 1.2577 - acc: 0.5662Epoch 00022: val_loss improved from 1.25889 to 1.21336, saving model to ./checkpoints/weights.022-1.2134.hdf5
3072/3021 [==============================] - 66s - loss: 1.2548 - acc: 0.5677 - val_loss: 1.2134 - val_acc: 0.6029
Epoch 24/300
2983/3021 [============================>.] - ETA: 0s - loss: 1.2721 - acc: 0.5686Epoch 00023: val_loss did not improve
3047/3021 [==============================] - 66s - loss: 1.2724 - acc: 0.5671 - val_loss: 1.2374 - val_acc: 0.5794
Epoch 25/300
3008/3021 [============================>.] - ETA: 0s - loss: 1.2293 - acc: 0.5854Epoch 00024: val_loss did not improve
3072/3021 [==============================] - 66s - loss: 1.2300 - acc: 0.5853 - val_loss: 1.3095 - val_acc: 0.5326
Epoch 26/300
3008/3021 [============================>.] - ETA: 0s - loss: 1.2368 - acc: 0.5665Epoch 00025: val_loss did not improve
3072/3021 [==============================] - 66s - loss: 1.2331 - acc: 0.5684 - val_loss: 1.2492 - val_acc: 0.5820
Epoch 27/300
2983/3021 [============================>.] - ETA: 0s - loss: 1.2670 - acc: 0.5595Epoch 00026: val_loss did not improve
3047/3021 [==============================] - 66s - loss: 1.2659 - acc: 0.5606 - val_loss: 1.2254 - val_acc: 0.6042
Epoch 28/300
3008/3021 [============================>.] - ETA: 0s - loss: 1.2308 - acc: 0.5658Epoch 00027: val_loss did not improve
3072/3021 [==============================] - 66s - loss: 1.2292 - acc: 0.5661 - val_loss: 1.2818 - val_acc: 0.5365
Epoch 29/300
3008/3021 [============================>.] - ETA: 0s - loss: 1.2196 - acc: 0.5751Epoch 00028: val_loss improved from 1.21336 to 1.20770, saving model to ./checkpoints/weights.028-1.2077.hdf5
3072/3021 [==============================] - 66s - loss: 1.2217 - acc: 0.5749 - val_loss: 1.2077 - val_acc: 0.5794
Epoch 30/300
2983/3021 [============================>.] - ETA: 0s - loss: 1.2124 - acc: 0.5773Epoch 00029: val_loss did not improve
3047/3021 [==============================] - 66s - loss: 1.2105 - acc: 0.5776 - val_loss: 1.2598 - val_acc: 0.5273
Epoch 31/300
 640/3021 [=====>........................] - ETA: 41s - loss: 1.2084 - acc: 0.5672

In [24]:
#stg2 training

from keras.applications.vgg16 import VGG16

optimizer = Adam(lr=LearningRate)

base_model = VGG16(weights='imagenet', include_top=False)
# at this point, the top layers are well trained and we can start fine-tuning
# convolutional layers from inception V3. We will freeze the bottom N layers
# and train the remaining top layers.

# let's visualize layer names and layer indices to see how many layers
# we should freeze:
for i, layer in enumerate(base_model.layers):
   print(i, layer.name)

# we chose to train the top 2 inception blocks, i.e. we will freeze
# the first 172 layers and unfreeze the rest:
for layer in model.layers[:14]:
   layer.trainable = False
for layer in model.layers[14:]:
   layer.trainable = True

# we need to recompile the model for these modifications to take effect
# we use SGD with a low learning rate
model.compile(optimizer=optimizer, loss='categorical_crossentropy')

# we train our model again (this time fine-tuning the top 2 inception blocks
# alongside the top Dense layers
model.fit_generator(train_generator, samples_per_epoch=len(X_train), nb_epoch=300, verbose=1, 
                    callbacks=[early_stopping, model_checkpoint, learningrate_schedule, tensorboard], 
                    validation_data=valid_generator, nb_val_samples=len(X_valid), nb_worker=3, pickle_safe=True)


0 input_3
1 block1_conv1
2 block1_conv2
3 block1_pool
4 block2_conv1
5 block2_conv2
6 block2_pool
7 block3_conv1
8 block3_conv2
9 block3_conv3
10 block3_pool
11 block4_conv1
12 block4_conv2
13 block4_conv3
14 block4_pool
15 block5_conv1
16 block5_conv2
17 block5_conv3
18 block5_pool
Epoch 1/300
3008/3021 [============================>.] - ETA: 0s - loss: 1.2538
/opt/anaconda3/lib/python3.5/site-packages/keras/engine/training.py:1470: UserWarning: Epoch comprised more than `samples_per_epoch` samples, which might affect learning results. Set `samples_per_epoch` correctly to avoid this warning.
  warnings.warn('Epoch comprised more than '
Epoch 00000: val_loss improved from inf to 1.04548, saving model to ./checkpoints/weights.000-1.0455.hdf5
3072/3021 [==============================] - 85s - loss: 1.2478 - val_loss: 1.0455
Epoch 2/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.9460Epoch 00001: val_loss improved from 1.04548 to 0.80567, saving model to ./checkpoints/weights.001-0.8057.hdf5
3072/3021 [==============================] - 70s - loss: 0.9437 - val_loss: 0.8057
Epoch 3/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.7676Epoch 00002: val_loss did not improve
3047/3021 [==============================] - 71s - loss: 0.7734 - val_loss: 0.9774
Epoch 4/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.6906Epoch 00003: val_loss improved from 0.80567 to 0.63040, saving model to ./checkpoints/weights.003-0.6304.hdf5
3072/3021 [==============================] - 70s - loss: 0.6909 - val_loss: 0.6304
Epoch 5/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.5838Epoch 00004: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.5813 - val_loss: 0.7017
Epoch 6/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.5608Epoch 00005: val_loss improved from 0.63040 to 0.61503, saving model to ./checkpoints/weights.005-0.6150.hdf5
3047/3021 [==============================] - 69s - loss: 0.5596 - val_loss: 0.6150
Epoch 7/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.5016Epoch 00006: val_loss improved from 0.61503 to 0.53570, saving model to ./checkpoints/weights.006-0.5357.hdf5
3072/3021 [==============================] - 70s - loss: 0.4975 - val_loss: 0.5357
Epoch 8/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.4361Epoch 00007: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.4361 - val_loss: 0.5432
Epoch 9/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.4059Epoch 00008: val_loss did not improve
3047/3021 [==============================] - 69s - loss: 0.4032 - val_loss: 0.5752
Epoch 10/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.3733Epoch 00009: val_loss improved from 0.53570 to 0.33159, saving model to ./checkpoints/weights.009-0.3316.hdf5
3072/3021 [==============================] - 70s - loss: 0.3744 - val_loss: 0.3316
Epoch 11/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.3254Epoch 00010: val_loss did not improve
3072/3021 [==============================] - 69s - loss: 0.3243 - val_loss: 0.4274
Epoch 12/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.3495Epoch 00011: val_loss did not improve
3047/3021 [==============================] - 69s - loss: 0.3477 - val_loss: 0.4750
Epoch 13/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.3056Epoch 00012: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.3016 - val_loss: 0.5004
Epoch 14/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.2465Epoch 00013: val_loss improved from 0.33159 to 0.26357, saving model to ./checkpoints/weights.013-0.2636.hdf5
3072/3021 [==============================] - 70s - loss: 0.2496 - val_loss: 0.2636
Epoch 15/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.2556Epoch 00014: val_loss did not improve
3047/3021 [==============================] - 70s - loss: 0.2575 - val_loss: 0.3512
Epoch 16/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.2091Epoch 00015: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.2092 - val_loss: 0.3096
Epoch 17/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.2256Epoch 00016: val_loss improved from 0.26357 to 0.22867, saving model to ./checkpoints/weights.016-0.2287.hdf5
3072/3021 [==============================] - 70s - loss: 0.2235 - val_loss: 0.2287
Epoch 18/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.2045Epoch 00017: val_loss did not improve
3047/3021 [==============================] - 70s - loss: 0.2033 - val_loss: 0.2895
Epoch 19/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.1903Epoch 00018: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.1889 - val_loss: 0.2927
Epoch 20/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.1784Epoch 00019: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.1772 - val_loss: 0.3048
Epoch 21/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.1967Epoch 00020: val_loss did not improve
3047/3021 [==============================] - 69s - loss: 0.1949 - val_loss: 0.3690
Epoch 22/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.1538Epoch 00021: val_loss did not improve
3072/3021 [==============================] - 71s - loss: 0.1568 - val_loss: 0.3551
Epoch 23/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.1695Epoch 00022: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.1685 - val_loss: 0.3090
Epoch 24/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.1290Epoch 00023: val_loss did not improve
3047/3021 [==============================] - 69s - loss: 0.1279 - val_loss: 0.2879
Epoch 25/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.1346Epoch 00024: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.1344 - val_loss: 0.2958
Epoch 26/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.1212Epoch 00025: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.1207 - val_loss: 0.3149
Epoch 27/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.1540Epoch 00026: val_loss did not improve
3047/3021 [==============================] - 70s - loss: 0.1538 - val_loss: 0.3790
Epoch 28/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.1096Epoch 00027: val_loss did not improve

Epoch 00027: reducing learning rate to 1.9999999494757503e-05.
3072/3021 [==============================] - 70s - loss: 0.1103 - val_loss: 0.2986
Epoch 29/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0657Epoch 00028: val_loss improved from 0.22867 to 0.20805, saving model to ./checkpoints/weights.028-0.2080.hdf5
3072/3021 [==============================] - 70s - loss: 0.0655 - val_loss: 0.2080
Epoch 30/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.0542Epoch 00029: val_loss did not improve
3047/3021 [==============================] - 70s - loss: 0.0550 - val_loss: 0.2437
Epoch 31/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0487Epoch 00030: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0499 - val_loss: 0.2660
Epoch 32/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0522Epoch 00031: val_loss improved from 0.20805 to 0.20593, saving model to ./checkpoints/weights.031-0.2059.hdf5
3072/3021 [==============================] - 71s - loss: 0.0518 - val_loss: 0.2059
Epoch 33/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.0492Epoch 00032: val_loss did not improve
3047/3021 [==============================] - 70s - loss: 0.0486 - val_loss: 0.2317
Epoch 34/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0539Epoch 00033: val_loss improved from 0.20593 to 0.17129, saving model to ./checkpoints/weights.033-0.1713.hdf5
3072/3021 [==============================] - 70s - loss: 0.0542 - val_loss: 0.1713
Epoch 35/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0475Epoch 00034: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0474 - val_loss: 0.2162
Epoch 36/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.0455Epoch 00035: val_loss did not improve
3047/3021 [==============================] - 69s - loss: 0.0465 - val_loss: 0.2032
Epoch 37/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0410Epoch 00036: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0404 - val_loss: 0.2118
Epoch 38/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0423Epoch 00037: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0420 - val_loss: 0.1720
Epoch 39/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.0413Epoch 00038: val_loss did not improve
3047/3021 [==============================] - 70s - loss: 0.0407 - val_loss: 0.2387
Epoch 40/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0350Epoch 00039: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0348 - val_loss: 0.2889
Epoch 41/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0315Epoch 00040: val_loss did not improve
3072/3021 [==============================] - 69s - loss: 0.0326 - val_loss: 0.2150
Epoch 42/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.0361Epoch 00041: val_loss did not improve
3047/3021 [==============================] - 69s - loss: 0.0357 - val_loss: 0.2293
Epoch 43/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0305Epoch 00042: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0301 - val_loss: 0.1747
Epoch 44/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0385Epoch 00043: val_loss improved from 0.17129 to 0.17055, saving model to ./checkpoints/weights.043-0.1705.hdf5
3072/3021 [==============================] - 70s - loss: 0.0386 - val_loss: 0.1705
Epoch 45/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.0363Epoch 00044: val_loss did not improve

Epoch 00044: reducing learning rate to 3.999999898951501e-06.
3047/3021 [==============================] - 69s - loss: 0.0360 - val_loss: 0.2691
Epoch 46/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0322Epoch 00045: val_loss improved from 0.17055 to 0.15367, saving model to ./checkpoints/weights.045-0.1537.hdf5
3072/3021 [==============================] - 70s - loss: 0.0320 - val_loss: 0.1537
Epoch 47/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0282Epoch 00046: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0280 - val_loss: 0.2337
Epoch 48/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.0262Epoch 00047: val_loss did not improve
3047/3021 [==============================] - 70s - loss: 0.0263 - val_loss: 0.2304
Epoch 49/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0219Epoch 00048: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0217 - val_loss: 0.2491
Epoch 50/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0351Epoch 00049: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0370 - val_loss: 0.1880
Epoch 51/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.0267Epoch 00050: val_loss improved from 0.15367 to 0.13505, saving model to ./checkpoints/weights.050-0.1350.hdf5
3047/3021 [==============================] - 70s - loss: 0.0265 - val_loss: 0.1350
Epoch 52/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0243Epoch 00051: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0257 - val_loss: 0.2410
Epoch 53/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0243Epoch 00052: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0251 - val_loss: 0.2465
Epoch 54/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.0250Epoch 00053: val_loss did not improve
3047/3021 [==============================] - 69s - loss: 0.0262 - val_loss: 0.1908
Epoch 55/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0237Epoch 00054: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0233 - val_loss: 0.3144
Epoch 56/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0200Epoch 00055: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0198 - val_loss: 0.2361
Epoch 57/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.0180Epoch 00056: val_loss did not improve
3047/3021 [==============================] - 70s - loss: 0.0184 - val_loss: 0.1965
Epoch 58/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0246Epoch 00057: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0246 - val_loss: 0.1858
Epoch 59/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0248Epoch 00058: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0252 - val_loss: 0.2242
Epoch 60/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.0271Epoch 00059: val_loss did not improve
3047/3021 [==============================] - 70s - loss: 0.0270 - val_loss: 0.2275
Epoch 61/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0202Epoch 00060: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0201 - val_loss: 0.1711
Epoch 62/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0244Epoch 00061: val_loss did not improve

Epoch 00061: reducing learning rate to 7.999999979801942e-07.
3072/3021 [==============================] - 70s - loss: 0.0242 - val_loss: 0.1970
Epoch 63/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.0244Epoch 00062: val_loss did not improve
3047/3021 [==============================] - 69s - loss: 0.0242 - val_loss: 0.2559
Epoch 64/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0231Epoch 00063: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0233 - val_loss: 0.2054
Epoch 65/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0206Epoch 00064: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0204 - val_loss: 0.2215
Epoch 66/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.0249Epoch 00065: val_loss did not improve
3047/3021 [==============================] - 70s - loss: 0.0249 - val_loss: 0.1982
Epoch 67/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0198Epoch 00066: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0196 - val_loss: 0.2284
Epoch 68/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0235Epoch 00067: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0231 - val_loss: 0.2271
Epoch 69/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.0219Epoch 00068: val_loss did not improve
3047/3021 [==============================] - 70s - loss: 0.0216 - val_loss: 0.1576
Epoch 70/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0197Epoch 00069: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0193 - val_loss: 0.1911
Epoch 71/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0241Epoch 00070: val_loss did not improve
3021/3021 [==============================] - 69s - loss: 0.0241 - val_loss: 0.1895
Epoch 72/300
2970/3021 [============================>.] - ETA: 0s - loss: 0.0236Epoch 00071: val_loss did not improve

Epoch 00071: reducing learning rate to 1.600000018697756e-07.
3034/3021 [==============================] - 70s - loss: 0.0232 - val_loss: 0.2577
Epoch 73/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0229Epoch 00072: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0232 - val_loss: 0.1452
Epoch 74/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.0228Epoch 00073: val_loss did not improve
3047/3021 [==============================] - 70s - loss: 0.0226 - val_loss: 0.1755
Epoch 75/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0190Epoch 00074: val_loss improved from 0.13505 to 0.10881, saving model to ./checkpoints/weights.074-0.1088.hdf5
3072/3021 [==============================] - 70s - loss: 0.0196 - val_loss: 0.1088
Epoch 76/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0224Epoch 00075: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0230 - val_loss: 0.1868
Epoch 77/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.0224Epoch 00076: val_loss did not improve
3047/3021 [==============================] - 70s - loss: 0.0220 - val_loss: 0.1434
Epoch 78/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0238Epoch 00077: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0235 - val_loss: 0.2376
Epoch 79/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0172Epoch 00078: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0170 - val_loss: 0.1615
Epoch 80/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.0218Epoch 00079: val_loss did not improve
3047/3021 [==============================] - 69s - loss: 0.0227 - val_loss: 0.1963
Epoch 81/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0218Epoch 00080: val_loss did not improve
3072/3021 [==============================] - 69s - loss: 0.0228 - val_loss: 0.2624
Epoch 82/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0210Epoch 00081: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0209 - val_loss: 0.1959
Epoch 83/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.0222Epoch 00082: val_loss did not improve
3047/3021 [==============================] - 69s - loss: 0.0224 - val_loss: 0.1929
Epoch 84/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0184Epoch 00083: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0182 - val_loss: 0.1770
Epoch 85/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0184Epoch 00084: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0187 - val_loss: 0.1540
Epoch 86/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.0222Epoch 00085: val_loss did not improve

Epoch 00085: reducing learning rate to 3.199999980552093e-08.
3047/3021 [==============================] - 70s - loss: 0.0221 - val_loss: 0.1892
Epoch 87/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0206Epoch 00086: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0203 - val_loss: 0.1861
Epoch 88/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0197Epoch 00087: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0199 - val_loss: 0.1536
Epoch 89/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.0202Epoch 00088: val_loss did not improve
3047/3021 [==============================] - 70s - loss: 0.0208 - val_loss: 0.1542
Epoch 90/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0167Epoch 00089: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0165 - val_loss: 0.2076
Epoch 91/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0193Epoch 00090: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0191 - val_loss: 0.2714
Epoch 92/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.0205Epoch 00091: val_loss did not improve
3047/3021 [==============================] - 70s - loss: 0.0201 - val_loss: 0.1680
Epoch 93/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0217Epoch 00092: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0213 - val_loss: 0.1884
Epoch 94/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0248Epoch 00093: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0245 - val_loss: 0.1890
Epoch 95/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.0213Epoch 00094: val_loss did not improve
3047/3021 [==============================] - 69s - loss: 0.0212 - val_loss: 0.2831
Epoch 96/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0201Epoch 00095: val_loss did not improve

Epoch 00095: reducing learning rate to 6.399999818995639e-09.
3072/3021 [==============================] - 70s - loss: 0.0198 - val_loss: 0.2074
Epoch 97/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0164Epoch 00096: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0164 - val_loss: 0.2718
Epoch 98/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.0218Epoch 00097: val_loss did not improve
3047/3021 [==============================] - 70s - loss: 0.0214 - val_loss: 0.2625
Epoch 99/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0208Epoch 00098: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0209 - val_loss: 0.1456
Epoch 100/300
3008/3021 [============================>.] - ETA: 0s - loss: 0.0216Epoch 00099: val_loss did not improve
3072/3021 [==============================] - 70s - loss: 0.0214 - val_loss: 0.2114
Epoch 101/300
2983/3021 [============================>.] - ETA: 0s - loss: 0.0203Epoch 00100: val_loss did not improve
3047/3021 [==============================] - 69s - loss: 0.0200 - val_loss: 0.1885
Epoch 00100: early stopping
Out[24]:
<keras.callbacks.History at 0x7f6e5ab542e8>

In [ ]:
#resume training

files = glob.glob('./checkpoints/*')
val_losses = [float(f.split('-')[-1][:-5]) for f in files]
index = val_losses.index(min(val_losses))
print('Loading model from checkpoints file ' + files[index])
model = load_model(files[index])

model.fit_generator(train_generator, samples_per_epoch=len(X_train), nb_epoch=300, verbose=1, 
                    callbacks=[early_stopping, model_checkpoint, learningrate_schedule, tensorboard], 
                    validation_data=valid_generator, nb_val_samples=len(X_valid), nb_worker=3, pickle_safe=True)


Loading model from checkpoints file ./checkpoints/weights.028-1.2077.hdf5
Epoch 1/300
3008/3021 [============================>.] - ETA: 0s - loss: 1.2688 - acc: 0.5455
/opt/anaconda3/lib/python3.5/site-packages/keras/engine/training.py:1470: UserWarning: Epoch comprised more than `samples_per_epoch` samples, which might affect learning results. Set `samples_per_epoch` correctly to avoid this warning.
  warnings.warn('Epoch comprised more than '
Epoch 00000: val_loss improved from inf to 1.20833, saving model to ./checkpoints/weights.000-1.2083.hdf5
3072/3021 [==============================] - 82s - loss: 1.2664 - acc: 0.5472 - val_loss: 1.2083 - val_acc: 0.5938
Epoch 2/300
 832/3021 [=======>......................] - ETA: 37s - loss: 1.2594 - acc: 0.5601

In [38]:
#test submission

import datetime

if os.path.exists('../data/data_test_{}_{}.pickle'.format(ROWS, COLS)):
    print ('Exist data_test_{}_{}.pickle. Loading test data from file.'.format(ROWS, COLS))
    with open('../data/data_test_{}_{}.pickle'.format(ROWS, COLS), 'rb') as f:
        data_test = pickle.load(f)
    X_test = data_test['X_test']
    test_files = data_test['test_files']
else:
    print ('Loading test data from original images. Generating data_test_{}_{}.pickle.'.format(ROWS, COLS))

    test_files = [im for im in os.listdir(TEST_DIR)]
    X_test = np.ndarray((len(test_files), ROWS, COLS, 3), dtype=np.uint8)

    for i, im in enumerate(test_files): 
        X_test[i] = read_image(TEST_DIR+im)
        if i%300 == 0: print('Processed {} of {}'.format(i, len(test_files)))
            
    data_test = {'X_test': X_test,'test_files': test_files }
    
    with open('../data/data_test_{}_{}.pickle'.format(ROWS, COLS), 'wb') as f:
        pickle.dump(data_test, f)
            
X_test = X_test / 255.

files = glob.glob('./checkpoints/*')
val_losses = [float(f.split('-')[-1][:-5]) for f in files]
index = val_losses.index(min(val_losses))
model = load_model(files[index])

test_preds = model.predict(X_test, batch_size=BatchSize, verbose=1)
#test_preds= test_preds / np.sum(test_preds,axis=1,keepdims=True)

submission = pd.DataFrame(test_preds, columns=FISH_CLASSES)
#submission.loc[:, 'image'] = pd.Series(test_files, index=submission.index)
submission.insert(0, 'image', test_files)

now = datetime.datetime.now()
info = 'VGG16TF_' + '{:.4f}'.format(min(val_losses))
sub_file = 'submission_' + info + '_' + str(now.strftime("%Y-%m-%d-%H-%M")) + '.csv'
submission.to_csv(sub_file, index=False)


Exist data_test_224_224.pickle. Loading test data from file.
1000/1000 [==============================] - 28s    
###clear checkpoints folder if not os.path.exists('./checkpoints'): os.mkdir('./checkpoints') files = glob.glob('./checkpoints/*') for f in files: os.remove(f)
###clear logs folder if not os.path.exists('./logs'): os.mkdir('./logs') files = glob.glob('./logs/*') for f in files: os.remove(f)

In [ ]: